@extends('admin.layout.main')
@section('content')
    <div class="layui-tab layui-tab-brief" lay-filter="demoTitle">
        <ul class="layui-tab-title site-demo-title">
            <li class="layui-this"><a href="javascript:location.reload()">专题管理</a></li>
        </ul>
        <div class="layui-body layui-tab-content site-demo site-demo-body">
            <div class="layui-main">
                <a class="layui-btn" href="/admin/topics/create">添加</a>
            </div>

            <div class="layui-tab-item layui-show">
                <div class="layui-main">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                        <legend>专题管理</legend>
                    </fieldset>
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>专题名</th>
                            <th>创建时间</th>
                            <th>修改时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        @foreach($topics as $topic)
                            <tr>
                                <td>{{$topic->id}}</td>
                                <td>{{$topic->name}}</td>
                                <td>{{$topic->created_at}}</td>
                                <td>{{$topic->updated_at}}</td>
                                <td><a class="layui-btn layui-btn-danger resource-delete"
                                       href="javascrip:;" delete-url="/admin/topics/{{$topic->id}}">删除</a>
                                </td>
                            </tr>
                        @endforeach
                    </table>

                </div>
            </div>
        </div>
    </div>
    <script>
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $(".resource-delete").click(function (event) {
            if (confirm("确定执行删除操作么?") == false) {
                return;
            }

            var target = $(event.target);

            event.preventDefault();
            var url = $(target).attr("delete-url");
            $.ajax({
                url: url,
                method: "POST",
                data: { "_method": 'DELETE'},
                dataType: "json",
                success: function success(data) {
                    if (data.error != 0) {
                        alert(data.msg);
                        return;
                    }
                    window.location.reload();
                }
            });
        });
    </script>
@endsection

